//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.TridentWordAddIn.Common;
using System.IO;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// The class FileInfoUserControl
    /// </summary>
    public partial class FileInfoUserControl : BaseTypeInitializer
    {
        private bool isFileInfo = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileInfoUserControl"/> class.
        /// </summary>
        public FileInfoUserControl(bool isFileInfo)
        {
            InitializeComponent();
            this.isFileInfo = isFileInfo;
            if (!this.isFileInfo)
            {
                lblSelectFile.Text = "Specify or Browse to Directory";
            }
        }

        /// <summary>
        /// Sets the result.
        /// </summary>
        /// <param name="result">The result.</param>
        public override void SetResult(object result)
        {
            this.txtFilePath.Text = result.ToString();     
        }

        /// <summary>
        /// Gets the result.
        /// </summary>
        /// <returns></returns>
        public override object GetResult()
        {
            return this.txtFilePath.Text;
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid
        {
            get
            {
                if (this.isFileInfo)
                {
                    return File.Exists(this.txtFilePath.Text);
                }
                else
                {
                    return Directory.Exists(this.txtFilePath.Text);
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (this.isFileInfo)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "All Files (*.*)|*.*";
                if(openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.txtFilePath.Text = openFileDialog.FileName;
                }
            }
            else
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    this.txtFilePath.Text = folderBrowserDialog.SelectedPath;
                }
            }
        }
    }
}
